package com.grape.api;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.grape.annotation.OperateMethodName;
import com.grape.controller.BaseController;
import com.grape.dto.OnlineUserDTO;
import com.grape.dto.UserDetailDTO;
import com.grape.entity.SysPermission;
import com.grape.result.R;
import com.grape.vo.BaseVo;
import com.grape.vo.StartVO;
import com.grape.vo.StopVO;
import com.grape.entity.SysUser;
import com.grape.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import java.util.List;


/**
 * 用户信息表 控制器
 * @author  putao
 * @email   2628279194@qq.com
 */
@RestController
@RequestMapping("users")
@Api(tags = "用户接口")
@AllArgsConstructor
public class SysUserController extends BaseController<SysUser> {

    private final ISysUserService sysUserService;

    @GetMapping
    @ApiOperation(value = "用户信息表列表")
    @OperateMethodName(value = "用户信息表列表", url = "system:users:listPage")
    public R<?> listPage(BaseVo baseVo) {
        PageInfo<SysUser> pageInfo = sysUserService.userListPage(baseVo);
        return CollectionUtil.isNotEmpty(pageInfo.getList()) ? selectSuccess(pageInfo, baseVo) : selectNotFound(pageInfo, baseVo);
    }

    @PostMapping("logout")
    @ApiOperation(value = "退出登录")
    @OperateMethodName(value = "退出登录", url = "system:users:logout")
    public R<?> logout() {
       sysUserService.logout();
        return R.logoutSuccess(null, null);
    }

    @PostMapping("updateAvatar")
    @ApiOperation(value = "更换头像")
    @OperateMethodName(value = "更换头像", url = "system:users:updateAvatar")
    public R<?> updateAvatar(MultipartFile file) throws Exception {
        String path = sysUserService.updateAvatar(file);
        return R.operationSuccess(path, null);
    }


    @GetMapping("listPageCustom")
    @ApiOperation(value = "用户信息表自定义分页")
    @OperateMethodName(value = "用户信息表自定义分页", url = "system:users:listPageCustom")
    public R<?> listPageCustom(BaseVo baseVo) {
        Page<SysUser> pages = PageHelper.startPage(baseVo.getPageNum(), baseVo.getPageSize());
        List<SysUser> sysUserList = sysUserService.listPageCustom(baseVo);
        PageInfo<SysUser> pageInfo = pages.toPageInfo();
        return CollectionUtil.isNotEmpty(sysUserList) ? selectSuccess(pageInfo, baseVo) : selectNotFound(pageInfo, baseVo);
    }

    @PutMapping
    @ApiOperation(value = "用户修改")
    @OperateMethodName(value = "用户信息表修改", url = "system:users:update")
    public R<?> update(@Validated @RequestBody SysUser sysUser){
        if (!Strings.isNotBlank(sysUser.getId())) {
            return updateNotFound(sysUser, null);
        }
        sysUserService.updateData(sysUser);
        return updateSuccess(null, null);
    }

    @DeleteMapping
    @ApiOperation(value = "删除用户，可批量多个id逗号拼接")
    @OperateMethodName(value = "用户信息表删除,批量", url = "system:users:delete")
    public R<?> delete(@NotBlank(message = "删除时ids不能为空")  @RequestParam String ids){
        int number = sysUserService.deleteData(ids);
        if (number < 0) {
            return deleteNotFound(ids, number);
        }
        return deleteSuccess(ids, null);
    }

    @PostMapping("logged")
    @ApiOperation(value = "注销账号")
    @OperateMethodName(value = "注销账号", url = "system:users:logged")
    public R<?> logged() {
        sysUserService.logged();
        return R.operationSuccess(null, null);
    }

    @PostMapping("stop")
    @ApiOperation(value = "停用账号")
    @OperateMethodName(value = "停用账号", url = "system:users:stop")
    public R<?> stop(@Validated @RequestBody StopVO stopVo) {
        sysUserService.stop(stopVo);
        return R.operationSuccess(null, null);
    }

    @PostMapping("start")
    @ApiOperation(value = "启用账号")
    @OperateMethodName(value = "启用账号", url = "system:users:start")
    public R<?> start(@RequestBody StartVO startVo) {
        sysUserService.start(startVo);
        return R.operationSuccess(null, null);
    }


    @GetMapping("getOnlineUserList")
    @ApiOperation(value = "查询在线用户")
    @OperateMethodName(value = "查询在线用户", url = "system:users:getOnlineUserList")
    public R<?> getOnlineUserList() {
        List<OnlineUserDTO> onlinedUserList = sysUserService.onlineUserList();
        PageInfo<OnlineUserDTO> pageInfo = PageInfo.of(onlinedUserList, 2);
        return selectSuccess(pageInfo, null);
    }

    @GetMapping("detailDTO")
    @ApiOperation(value = "获取当前用户信息")
    @OperateMethodName(value = "获取当前用户信息", url = "system:users:detailDTO")
    public R<UserDetailDTO> detailDTO() {
        return selectSuccess(sysUserService.detailDTO(), null);
    }

    @GetMapping("getByUsername")
    @ApiOperation(value = "根据用户名查询用户")
    @OperateMethodName(value = "根据用户名查询用户", url = "system:users:getByUsername")
    public R<SysUser> getByUsername(@NotBlank(message = "用户名不能为空") @RequestParam String username) {
        return selectSuccess(sysUserService.getByUsername(username), null);
    }

    @GetMapping("getByPhone")
    @ApiOperation(value = "根据手机号码查询用户")
    @OperateMethodName(value = "根据手机号码查询用户", url = "system:users:getByPhone")
    public R<SysUser> getByPhone(@NotBlank(message = "手机号码不能为空") @RequestParam String phone) {
        return selectSuccess(sysUserService.getByPhone(phone), null);
    }

    @GetMapping("getByUserId")
    @ApiOperation(value = "根据用户id查询用户")
    @OperateMethodName(value = "根据用户id查询用户", url = "system:users:getByUserId")
    public R<SysUser> getByUserId() {
        return selectSuccess(sysUserService.getByUserId(), null);
    }

    @GetMapping("getPermissionByUserId")
    @ApiOperation(value = "根据手机号码查询用户")
    @OperateMethodName(value = "根据手机号码查询用户", url = "system:users:getPermissionByUserId")
    public R<List<SysPermission>> getPermissionByUserId() {
        return selectSuccess(sysUserService.getPermissionByUserId(), null);
    }

}



